/**
 * @file HelpDialog.cpp
 */

#include <HelpDialog.hpp>

#include <iostream>
#include <qaction.h>
#include <qstandarditemmodel.h>
using namespace std;

#include <images/images.h>

HelpDialog::HelpDialog(const string &config) :
		QDialog() {
	this->_config = new HelpConfigManager(config);
	if(this->_config->title() != "") {
		this->setWindowTitle(this->_config->title().c_str());
	} else {
		this->setWindowTitle(XADOS_PROGRAM" "XADOS_VERSION" Help");
	}
	if(this->_config->icon() != "") {
		this->setWindowIcon(QPixmap(this->_config->icon().c_str()));
	}
	this->_hasTree = false;

	this->_search = new QLineEdit(this);
	QObject::connect(this->_search, SIGNAL(textEdited(const QString&)), this, SLOT(searchPage(const QString&)));

	this->_show = new QPushButton("&Show",this);
	QObject::connect(this->_show, SIGNAL(pressed()), this, SLOT(showPage()));

	this->_tree = new QTreeView(this);
	this->_tree->setSelectionBehavior(QAbstractItemView::SelectItems);
	this->_tree->setSelectionMode(QAbstractItemView::SingleSelection);
	this->_tree->setEditTriggers(QAbstractItemView::NoEditTriggers);
	QObject::connect(this->_tree, SIGNAL(clicked(const QModelIndex&)), this, SLOT(changePage(const QModelIndex&)));

	this->_browser = new QTextBrowser(this);

	this->_close = new QPushButton(this);
	this->_close->setText("&Close");
	QObject::connect(this->_close, SIGNAL(pressed()), this, SLOT(close()));

	this->setGeometry(0, 0, 800, 600);

	if(this->_config->init() != "") {
		this->changePage(this->_config->init());
	}
}

HelpDialog::~HelpDialog() {
	delete this->_search;
	delete this->_show;
	delete this->_browser;
	delete this->_close;
	delete this->_tree;
}

void HelpDialog::changePage(const QModelIndex &index) {
	this->changePage(index.data(0).toString().toStdString());
}

void HelpDialog::changePage(const string &index) {
	HelpConfigManager::ItemInfoMap::const_iterator itemsIt = this->_config->items().find(index);
	if(itemsIt != this->_config->items().end()) {
		QString url = this->_config->config().c_str();
		url += "/";
		url += itemsIt->second.url.c_str();

		this->_browser->setSource(QUrl(url));
	} else {
		cerr << XADOS_PROGRAM": Item '" << index << "' not found." << endl;
	}
}

void HelpDialog::createTree() {
	this->_hasTree = true;

	QStandardItemModel* model = new QStandardItemModel(this->_tree);
	this->_tree->setModel(model);

	const HelpConfigManager::ItemInfoMap &items = this->_config->items();
	HelpConfigManager::ItemInfoMap::const_iterator itemsIt;
	/*
	 * Loading no child items.
	 */
	vector<string> roots;
	for(itemsIt = items.begin(); itemsIt != items.end(); itemsIt++) {
		if(!itemsIt->second.isChild) {
			roots.push_back(itemsIt->first);
		}
	}

	QStandardItem* branch = NULL;
	QStandardItem* leaf = NULL;
	for(vector<string>::iterator i = roots.begin(); i != roots.end(); i++) {
		HelpConfigManager::ItemInfoMap::const_iterator item = items.find(*i);
		branch = new QStandardItem(QPixmap(xpmFolderPic), item->second.name.c_str());
		this->_topics.append(item->second.name.c_str());

		for(itemsIt = items.begin(); itemsIt != items.end(); itemsIt++) {
			if(itemsIt->second.parent == *i) {
				leaf = new QStandardItem(QPixmap(xpmFilePic), itemsIt->second.name.c_str());
				branch->appendRow(leaf);
				this->_topics.append(itemsIt->second.name.c_str());
			}
		}

		model->appendRow(branch);
	}
	model->setHeaderData(0,Qt::Horizontal,QVariant("Topics"));
}

void HelpDialog::resetUrl() {
	///@todo
	//this->_browser->setSource(QUrl("/home/raco/workspace/Xados2/help/help/init.html"));
}

void HelpDialog::resizeEvent(QResizeEvent* event) {
	this->QDialog::resizeEvent(event);
	this->_search->setGeometry(0, 0, 200, 25);
	this->_tree->setGeometry(0, 25, 200, this->height() - 75);
	this->_show->setGeometry(0, this->height()-50, 200, 25);
	this->_browser->setGeometry(200, 0, this->width() - 200, this->height() - 25);
	this->_close->setGeometry(this->width() - 100, this->height() - 25, 100, 25);
}

void HelpDialog::showEvent(QShowEvent* event) {
	this->QDialog::showEvent(event);
	this->resetUrl();

	if(!this->_hasTree) {
		this->createTree();
	}
}

void HelpDialog::searchPage(const QString &pattern) {
	if(pattern != "") {
		QStringList founds=this->_topics.filter(pattern, Qt::CaseInsensitive);
		if(founds.count()){
			this->changePage(founds.at(0).toStdString());
		}
	}
}

void HelpDialog::showPage() {
	QModelIndexList list = this->_tree->selectionModel()->selectedRows(0);
	if(list.size()) {
		this->changePage(list.at(0).data().toString().toStdString());
	}
}
